std\sys\anonymous_pipe/
windows.rs

1use crate::io::{self, PipeReader, PipeWriter};
2use crate::os::windows::io::{
3    AsHandle, AsRawHandle, BorrowedHandle, FromRawHandle, IntoRawHandle, OwnedHandle, RawHandle,
4};
5use crate::process::Stdio;
6use crate::ptr;
7use crate::sys::c;
8use crate::sys::handle::Handle;
9use crate::sys_common::{FromInner, IntoInner};
10
11pub type AnonPipe = Handle;
12
13pub fn pipe() -> io::Result<(AnonPipe, AnonPipe)> {
14    let mut read_pipe = c::INVALID_HANDLE_VALUE;
15    let mut write_pipe = c::INVALID_HANDLE_VALUE;
16
17    let ret = unsafe { c::CreatePipe(&mut read_pipe, &mut write_pipe, ptr::null_mut(), 0) };
18
19    if ret == 0 {
20        Err(io::Error::last_os_error())
21    } else {
22        unsafe { Ok((Handle::from_raw_handle(read_pipe), Handle::from_raw_handle(write_pipe))) }
23    }
24}
25
26#[unstable(feature = "anonymous_pipe", issue = "127154")]
27impl AsHandle for PipeReader {
28    fn as_handle(&self) -> BorrowedHandle<'_> {
29        self.0.as_handle()
30    }
31}
32#[unstable(feature = "anonymous_pipe", issue = "127154")]
33impl AsRawHandle for PipeReader {
34    fn as_raw_handle(&self) -> RawHandle {
35        self.0.as_raw_handle()
36    }
37}
38
39#[unstable(feature = "anonymous_pipe", issue = "127154")]
40impl FromRawHandle for PipeReader {
41    unsafe fn from_raw_handle(raw_handle: RawHandle) -> Self {
42        unsafe { Self(Handle::from_raw_handle(raw_handle)) }
43    }
44}
45#[unstable(feature = "anonymous_pipe", issue = "127154")]
46impl IntoRawHandle for PipeReader {
47    fn into_raw_handle(self) -> RawHandle {
48        self.0.into_raw_handle()
49    }
50}
51
52#[unstable(feature = "anonymous_pipe", issue = "127154")]
53impl From<PipeReader> for OwnedHandle {
54    fn from(pipe: PipeReader) -> Self {
55        Handle::into_inner(pipe.0)
56    }
57}
58#[unstable(feature = "anonymous_pipe", issue = "127154")]
59impl From<PipeReader> for Stdio {
60    fn from(pipe: PipeReader) -> Self {
61        Self::from(OwnedHandle::from(pipe))
62    }
63}
64
65#[unstable(feature = "anonymous_pipe", issue = "127154")]
66impl AsHandle for PipeWriter {
67    fn as_handle(&self) -> BorrowedHandle<'_> {
68        self.0.as_handle()
69    }
70}
71#[unstable(feature = "anonymous_pipe", issue = "127154")]
72impl AsRawHandle for PipeWriter {
73    fn as_raw_handle(&self) -> RawHandle {
74        self.0.as_raw_handle()
75    }
76}
77
78#[unstable(feature = "anonymous_pipe", issue = "127154")]
79impl FromRawHandle for PipeWriter {
80    unsafe fn from_raw_handle(raw_handle: RawHandle) -> Self {
81        unsafe { Self(Handle::from_raw_handle(raw_handle)) }
82    }
83}
84#[unstable(feature = "anonymous_pipe", issue = "127154")]
85impl IntoRawHandle for PipeWriter {
86    fn into_raw_handle(self) -> RawHandle {
87        self.0.into_raw_handle()
88    }
89}
90
91#[unstable(feature = "anonymous_pipe", issue = "127154")]
92impl From<PipeWriter> for OwnedHandle {
93    fn from(pipe: PipeWriter) -> Self {
94        Handle::into_inner(pipe.0)
95    }
96}
97#[unstable(feature = "anonymous_pipe", issue = "127154")]
98impl From<PipeWriter> for Stdio {
99    fn from(pipe: PipeWriter) -> Self {
100        Self::from(OwnedHandle::from(pipe))
101    }
102}
103
104#[unstable(feature = "anonymous_pipe", issue = "127154")]
105impl From<OwnedHandle> for PipeReader {
106    fn from(owned_handle: OwnedHandle) -> Self {
107        Self(Handle::from_inner(owned_handle))
108    }
109}
110
111#[unstable(feature = "anonymous_pipe", issue = "127154")]
112impl From<OwnedHandle> for PipeWriter {
113    fn from(owned_handle: OwnedHandle) -> Self {
114        Self(Handle::from_inner(owned_handle))
115    }
116}